﻿//using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System.IO;
using System.Security.Cryptography;

namespace BRS.BidModule
{
    public partial class frmProjectBidder : Form
    {
        int data;
        tb_Project pro;
        Entities brs = new Entities();
        List<tb_Bidder> bid;
        static HSSFWorkbook hssfworkbook;
        public frmProjectBidder(int id)
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
            pro = brs.tb_Project.Find(id);//通过id ，中间表，寻找当前项目下的投标人
            var prodate = pro.Deaddate.Value.AddDays(-1);
            var list = brs.tb_ProjectDetail.Where(p => p.ProjectCode == pro.ProjectCode).Select(p => p.CompanyCode).Distinct().ToList();//
            bid = brs.tb_Bidder.Where(p => list.Contains(p.ISID.ToString())).ToList();//

            var bidderSearch = brs.tb_Bidder.Where(p => list.Contains(p.ISID.ToString())).Join(brs.BBInfo, a => a.ISID, c => c.BidderId, (a, c) => new
            {
                cid = a.ISID,
                company = a.Company,
                isblack = a.IsBlack,
                blacknote = a.BlackNote,
                a.CreateDate,
                c.BidderInfo.ProjectCode,
                uptime = c.BidderInfo.CreateDate.Value
            }).Where(p => p.uptime > prodate && p.ProjectCode == pro.ProjectCode).OrderBy(p => p.uptime).Distinct().ToList();
            //List<BidderGrid> alist = new List<BidderGrid>();
            //for (int i = 0; i < bidderSearch.Count(); i++)
            //{
            //    alist.Add(new BidderGrid()
            //    {

                   
            //    });
            //}

            dataGridView1.DataSource = bidderSearch;
            label2.Text = pro.ProjectName;
            label4.Text = pro.ProjectCode;
            label7.Text = pro.Tender;
            label9.Text = pro.TenderAgent;
            data = id;

        }

        static void InitializeWorkbook()
        {
            hssfworkbook = new HSSFWorkbook();

            //create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "fsnhggzyjyzx";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "fsnhggzyjyzx";
            hssfworkbook.SummaryInformation = si;
        }
        private void proexport_Click(object sender, EventArgs e)
        {
            string Excelpath = "C:\\";
            FolderBrowserDialog filepath = new FolderBrowserDialog();
            filepath.ShowDialog();
            if (filepath.SelectedPath != "") Excelpath = filepath.SelectedPath;
            var pro = brs.tb_Project.Find(data);//找到id
            var prodate = pro.Deaddate.Value.AddDays(-1);
            var list = brs.tb_ProjectDetail.Where(p => p.ProjectCode == pro.ProjectCode).Select(p => p.CompanyCode).ToList();
            var bidderSearch = brs.tb_Bidder.Where(p => list.Contains(p.ISID.ToString())).Join(brs.BBInfo, a => a.ISID, c => c.BidderId, (a, c) => new
            {
                cid = a.ISID,
                company = a.Company,
                a.CreateDate,
                c.BidderInfo.BidderType,
                c.BidderInfo.Name,
                c.BidderInfo.IDCode,
                c.BidderInfo.Phone,
                c.BidderInfo.ProjectCode,
                c.BidderInfo.CreateDate.Value
            }).OrderBy(p => p.cid).ToList();

            var bidder = bidderSearch.Where(p => p.Value > prodate && p.ProjectCode == pro.ProjectCode).ToList();
            //var list = new List<BidderInfo>();

            //var p1 = cc.Where(p => p.BidderType == "法定代表人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
            //if (p1 != null)
            //{
            //    list.Add(p1);
            //}
            //var p2 = cc.Where(p => p.BidderType == "企业负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
            //if (p2 != null)
            //{
            //    list.Add(p2);
            //}
            //var p3 = cc.Where(p => p.BidderType == "企业技术负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
            //if (p3 != null)
            //{
            //    list.Add(p3);
            //}
            //var p4 = cc.Where(p => p.BidderType == "项目负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
            //if (p4 != null)
            //{
            //    list.Add(p4);
            //}
            InitializeWorkbook();//初始化表格
            var sheet1 = hssfworkbook.CreateSheet("Sheet1");
            ICell cellTitle = sheet1.CreateRow(0).CreateCell(0);
            cellTitle.SetCellValue("特定人员签到表");
            IRow row;
            //ICell cell;
            row = sheet1.CreateRow(1);//以下几行为表头信息
            row.CreateCell(0).SetCellValue("项目名称:");
            row.CreateCell(1).SetCellValue(pro.ProjectName);
            row.CreateCell(2).SetCellValue("项目编码:");
            row.CreateCell(3).SetCellValue(pro.ProjectCode);
            row.CreateCell(4).SetCellValue("招标人:");
            row.CreateCell(5).SetCellValue(pro.Tender);
            row.CreateCell(6).SetCellValue("招标代理人:");
            row.CreateCell(7).SetCellValue(pro.TenderAgent);
            row = sheet1.CreateRow(2);
            row.CreateCell(1).SetCellValue("签到截止时间：" + pro.Deadline.ToString());

            row = sheet1.CreateRow(3);
            row.CreateCell(0).SetCellValue("序号");
            //row.CreateCell().SetCellValue("编号");
            row.CreateCell(1).SetCellValue("投标人名称");
            //row.CreateCell(2).SetCellValue("组织机构编码");
            row.CreateCell(3).SetCellValue("生成日期");
            row.CreateCell(4).SetCellValue("特定类型");
            row.CreateCell(5).SetCellValue("姓名");
            row.CreateCell(6).SetCellValue("身份证号码");
            row.CreateCell(7).SetCellValue("手机号码");

            for (int i = 0; i < bidder.Count(); i++)//填充数据
            {

                //if (bidder[i].CreateDate.ToShortTimeString() == pro.Deadline.Value.ToShortTimeString())
                {
                    row = sheet1.CreateRow(i + 4);
                    row.CreateCell(0);
                    //row.CreateCell().SetCellValue(bidder[i].cid);
                    row.CreateCell(1).SetCellValue(bidder[i].company);
                    //row.CreateCell(2).SetCellValue(bidder[i].ccode);
                    row.CreateCell(3).SetCellValue(bidder[i].Value.ToString());
                    //row.CreateCell(3).SetCellValue(bidder[i].CreateDate.ToString());
                    row.CreateCell(4).SetCellValue(bidder[i].BidderType);
                    row.CreateCell(5).SetCellValue(bidder[i].Name);
                    row.CreateCell(6).SetCellValue(bidder[i].IDCode);
                    row.CreateCell(7).SetCellValue(bidder[i].Phone); sheet1.AutoSizeColumn(1);
                    sheet1.AutoSizeColumn(2);
                    sheet1.AutoSizeColumn(3);
                    sheet1.AutoSizeColumn(4);
                    sheet1.AutoSizeColumn(5);
                    sheet1.AutoSizeColumn(6);
                    sheet1.AutoSizeColumn(7);
                }



                //sheet1.AutoSizeColumn(8);
            }
            int count = 1;
            for (int i = 0; i < bidder.Count(); i++)//合并单元格
            {
                int j = i + 1;
                CellRangeAddress region0 = new CellRangeAddress(i + 4, j + 3, 0, 0);
                CellRangeAddress region1 = new CellRangeAddress(i + 4, j + 3, 1, 1);
                CellRangeAddress region2 = new CellRangeAddress(i + 4, j + 3, 2, 2);
                CellRangeAddress region3 = new CellRangeAddress(i + 4, j + 3, 3, 3);
                //CellRangeAddress region4 = new CellRangeAddress(i + 4, j + 3, 4, 4);
                for (j = i + 1; j < bidder.Count(); j++)
                {
                    if (bidder[i].cid != bidder[j].cid)
                    {
                        //sheet1.SetDefaultColumnStyle();
                        sheet1.GetRow(i + 4).GetCell(0).SetCellValue(count);//添加序号
                        count++;//计数，合并单元格后的数目
                        i = j - 1;
                        break;
                    }
                    else
                    {
                        region0 = new CellRangeAddress(i + 4, j + 4, 0, 0);
                        region1 = new CellRangeAddress(i + 4, j + 4, 1, 1);
                        region2 = new CellRangeAddress(i + 4, j + 4, 2, 2);
                        region3 = new CellRangeAddress(i + 4, j + 4, 3, 3);
                        //region4 = new CellRangeAddress(i + 4, j + 4, 4, 4);
                    }

                }
                sheet1.AddMergedRegion(region0);
                sheet1.AddMergedRegion(region1);
                sheet1.AddMergedRegion(region2);
                sheet1.AddMergedRegion(region3);
                //sheet1.AddMergedRegion(region4);

            }

            int Rowcount = bidder.Count();
            sheet1.GetRow(Rowcount + 3).GetCell(0).SetCellValue(count);
            row = sheet1.CreateRow(Rowcount + 6);
            row.CreateCell(1).SetCellValue("共有" + count + "条单位信息");
            row.CreateCell(3).SetCellValue("表格生成时间：");
            row.CreateCell(4).SetCellValue(DateTime.Now.ToString());
            row = sheet1.CreateRow(Rowcount + 8);
            row.CreateCell(1).SetCellValue("招标人：");
            row.CreateCell(4).SetCellValue("招标代理：");
            row = sheet1.CreateRow(Rowcount + 10);
            row.CreateCell(1).SetCellValue("交易见证人：");
            row.CreateCell(4).SetCellValue("监督单位：");
            FileStream file = new FileStream(@Excelpath + "/" + pro.ProjectName + "投标单位表.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();


        }


        #region DataGridView数据显示到Excel
        /// <summary>      
        /// 打开Excel并将DataGridView控件中数据导出到Excel     
        /// </summary>      
        /// <param name="dgv">DataGridView对象 </param>      
        /// <param name="isShowExcle">是否显示Excel界面 </param>      
        /// <remarks>     
        /// add com "Microsoft Excel 11.0 Object Library"     
        /// using Excel=Microsoft.Office.Interop.Excel;     
        /// </remarks>     
        /// <returns> </returns>      
        //public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcel)
        //{
        //    if (dgv.Rows.Count == 0)
        //        return false;
        //    //建立Excel对象      
        //    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        //    excel.Application.Workbooks.Add(true);
        //    Range allRang = excel.Columns.Cells;
        //    allRang.AutoFit();
        //    excel.Visible = isShowExcel;

        //    //生成字段名称  
        //    excel.Cells[2, 1] = "项目名称";
        //    excel.Cells[2, 2] = pro.ProjectName == "" ? "" : pro.ProjectName;
        //    excel.Cells[2, 3] = "项目编码";
        //    excel.Cells[2, 4] = pro.ProjectCode == "" ? "" : pro.ProjectCode;
        //    excel.Cells[2, 5] = "招标人";
        //    excel.Cells[2, 6] = pro.Tender == "" ? "" : pro.Tender;
        //    excel.Cells[2, 7] = "招标代理人";
        //    excel.Cells[2, 8] = pro.TenderAgent == "" ? "" : pro.TenderAgent;
        //    for (int i = 0; i < dgv.ColumnCount - 3; i++)
        //    {
        //        excel.Cells[3, i + 1] = dgv.Columns[i].HeaderText;
        //    }
        //    //填充数据      
        //    for (int i = 0; i < dgv.RowCount - 1; i++)
        //    {
        //        for (int j = 0; j < dgv.ColumnCount - 3; j++)
        //        {
        //            if (dgv[j, i].ValueType == typeof(string))
        //            {
        //                excel.Cells[i + 4, j + 1] = "'" + dgv[j, i].Value.ToString();
        //            }
        //            else
        //            {
        //                excel.Cells[i + 4, j + 1] = dgv[j, i].Value.ToString();
        //            }
        //        }
        //    }
        //    int COUNT = dgv.RowCount - 1;
        //    excel.Cells[dgv.RowCount + 4, 1] = "数据条数：" + COUNT;
        //    excel.Cells[dgv.RowCount + 5, 1] = "招标人：";
        //    excel.Cells[dgv.RowCount + 5, 3] = "招标代理：";
        //    excel.Cells[dgv.RowCount + 7, 1] = "交易见证人：";
        //    excel.Cells[dgv.RowCount + 7, 3] = "监督单位：";
        //    return true;
        //}
        #endregion

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string data = this.dataGridView1.CurrentRow.Cells["ISID"].Value.ToString();
            int GetData = int.Parse(data);
            BidderListfrm bidderlist = new BidderListfrm(GetData);
            bidderlist.ShowDialog();
            //frmBidderDetail frmbidder = new frmBidderDetail(GetData);
            //frmbidder.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                dataGridView1.DataSource = bid.Where(p => (p.Company.Contains(textBox1.Text))).ToList();
            }
        }

    }
    public class BidderGrid
    {
        int cid { get; set; }
        string company { get; set; }
        string isblack { get; set; }
        string blacknote { get; set; }
        string CreateDate { get; set; }
        string ProjectCode { get; set; }
        string uptime { get; set; }
    }
}
